//
//  ZFBBusinessController.m
//  支付宝口碑
//
//  Created by heima on 16/6/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "UIColor+Addition.h"
#import "ZFBBusiness.h"
#import "ZFBBusinessCell.h"
#import "ZFBBusinessController.h"
#import "ZFBBusinessType.h"
#import "ZFBBusinessTypeView.h"

@interface ZFBBusinessController ()

@property (nonatomic, strong) NSArray *businessData;

@end

@implementation ZFBBusinessController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 加载数据
    self.businessData = [self loadBusinessData];

    // 初始化UI
    [self setupUI];
}

// 初始化UI
- (void)setupUI {
    [self setupTableHeaderView];
    [self setupTableFooterView];
}

// 设置尾部视图
- (void)setupTableFooterView {
    UIButton *moreButton = [[UIButton alloc] init];
    // 文字
    [moreButton setTitle:@"查看全部" forState:UIControlStateNormal];
    // 颜色
    [moreButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [moreButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];

    // 修改字体大小
    moreButton.titleLabel.font = [UIFont systemFontOfSize:14];

    // 设置背景颜色
    // 灰度 R = G = B
    [moreButton setBackgroundColor:[UIColor colorWithHex:0xf5f5f9]];

    // 设置大小
    moreButton.frame = CGRectMake(0, 0, 0, 44);

    // 设置button到footer上
    self.tableView.tableFooterView = moreButton;

    //    // 设置tableView的inset
    //    self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
    //    self.tableView.contentOffset = CGPointMake(0, -20);
}

// 设置头部视图
- (void)setupTableHeaderView {
    // 设置头部视图

    // 获取商家分类视图
    ZFBBusinessTypeView *typeHeaderView = [[ZFBBusinessTypeView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width / 2)];
    typeHeaderView.buinessTypeData = [self loadBusinessTypeData];

    // 320 : 850

    // 375 : ?

    // 375 : 993.5

    // 加载xib
    UINib *nib = [UINib nibWithNibName:@"ZFBTableHeaderView" bundle:nil];
    // 获取xib中的视图
    UIView *businessHeaderView = [nib instantiateWithOwner:nil options:nil][0];

    // 获取view的宽度
    CGFloat xibW = businessHeaderView.bounds.size.width;

    // 获取view的高度
    CGFloat xibH = businessHeaderView.bounds.size.height;

    // 获取tableView宽度
    CGFloat viewW = self.tableView.bounds.size.width;

    // 计算 Header 应该显示的高度
    CGFloat viewH = xibH * viewW / xibW;

    businessHeaderView.frame = CGRectMake(0, typeHeaderView.bounds.size.height, viewW, viewH);

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewW, CGRectGetMaxY(businessHeaderView.frame))];
    [headerView addSubview:typeHeaderView];
    [headerView addSubview:businessHeaderView];

    // 把视图放置到tableView的header上
    self.tableView.tableHeaderView = headerView;
}

// 某一组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.businessData.count;
}

// cell长啥样儿
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 注意点:!!!!!!!!-----如果要使用sc摆cell, 不要用系统cell内部的控件
    //    cell.textLabel.text = @"123";

    // 创建cell
    ZFBBusinessCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellid" forIndexPath:indexPath];

    // 获取数据
    ZFBBusiness *business = self.businessData[indexPath.row];

    // 赋值数据
    cell.business = business;

    // 返回
    return cell;
}

// 组头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"热门推荐";
}

// 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

// 解析数据
- (NSArray *)loadBusinessData {
    // 获取路径
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"business.plist" withExtension:nil];

    // 转化成临时数组
    NSArray *tempArray = [NSArray arrayWithContentsOfURL:url];

    // 初始化可变数组
    NSMutableArray *array = [NSMutableArray array];

    // 遍历临时数组获取字典
    for (NSDictionary *dict in tempArray) {
        // 字典转模型
        ZFBBusiness *b = [ZFBBusiness businessWithDict:dict];

        // 把模型添加到可变数组当中
        [array addObject:b];
    }

    // 返回
    return array.copy;
}

- (NSArray *)loadBusinessTypeData {
    // 1. 加载 plist
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"BusinessType.plist" withExtension:nil];
    NSArray *list = [NSArray arrayWithContentsOfURL:url];

    // 2. 遍历数组，字典转模型
    NSMutableArray *arrayM = [NSMutableArray array];
    for (NSDictionary *dict in list) {
        [arrayM addObject:[ZFBBusinessType businessTypeWithDict:dict]];
    }

    // 3. 返回数组
    return arrayM.copy;
}

@end